﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCDto.Chats.Message;
using BCService.Chats;

namespace BCTMSRESTful.Controllers.Chats
{
    [ApiExplorerSettings(GroupName = "Chats")]
    [Route("api/v1/Chats/[controller]")]
    public class MessagesController : ChatBaseController
    {
        private readonly IMessageService messageService;

        public MessagesController(IMessageService messageService)
        {
            this.messageService = messageService;
        }

        /// <summary>
        /// 获取消息列表。
        /// </summary>
        /// <response code="200">一组消息列表。</response>
        /// <param name="threadId">会话编号。</param>
        /// <param name="minMessageId">最小消息编号。</param>
        /// <param name="maxMessageId">最大消息编号。</param>
        /// <param name="count">返回消息的条数 (默认 50 条)。</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<MessageDto>), 200)]
        public IActionResult GetMessages([FromQuery(Name = "thread_id")] long threadId,
            [FromQuery(Name = "min_message_id")] long? minMessageId,
            [FromQuery(Name = "max_message_id")] long? maxMessageId,
            [FromQuery(Name = "count")] int count = 50)
        {
            var list = messageService.GetMessages(threadId, minMessageId, maxMessageId, count);

            return this.Ok(list);
        }

        /// <summary>
        /// 获取指定消息。
        /// </summary>
        /// <param name="messageId">消息编号。</param>
        /// <response code="200">指定的消息详情。</response>
        /// <response code="204">指定的消息不存在或已被删除。</response>
        /// <returns></returns>
        [HttpGet("{message_id}")]
        [ProducesResponseType(typeof(MessageDto), 200)]
        public IActionResult GetMessage([FromRoute(Name = "message_id")] long messageId)
        {
            var message = messageService.GetMessage(messageId);

            return this.Ok(message);
        }

        /// <summary>
        /// 创建新消息。
        /// </summary>
        /// <param name="requestDto"></param>
        /// <response code="201">消息已创建。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(MessageDto), 201)]
        public IActionResult Create([FromBody] MessageRequestDto requestDto)
        {
            var message = messageService.Create(requestDto);

            return this.Created(Url.Action(nameof(GetMessage), new { message_id = message.MessageId }), message);
        }

        /// <summary>
        /// 删除指定的消息。
        /// </summary>
        /// <param name="messageId">消息编号。</param>
        /// <response code="204">指定的消息不存在或已被删除。</response>
        /// <returns></returns>
        [HttpDelete("{message_id}")]
        public IActionResult Delete([FromRoute(Name = "message_id")] long messageId)
        {
            messageService.Delete(messageId);

            return this.NoContent();
        }

        /// <summary>
        /// 撤回指定的消息。
        /// </summary>
        /// <param name="messageId">消息编号。</param>
        /// <response code="200">指定的消息详情。</response>
        /// <response code="204">指定的消息不存在或已被删除。</response>
        /// <returns></returns>
        [HttpPost("{message_id}/[action]")]
        [ProducesResponseType(typeof(MessageDto), 200)]
        public IActionResult Revoke([FromRoute(Name = "message_id")] long messageId)
        {
            var message = messageService.Revoke(messageId);

            return this.Ok(message);
        }

        /// <summary>
        /// 回执指定的消息。
        /// </summary>
        /// <param name="messageId">消息编号。</param>
        /// <response code="200">指定的消息详情。</response>
        /// <response code="204">指定的消息不存在或已被删除。</response>
        /// <returns></returns>
        [HttpPost("{message_id}/[action]")]
        [ProducesResponseType(typeof(MessageDto), 200)]
        public IActionResult Receipt([FromRoute(Name = "message_id")] long messageId)
        {
            var message = messageService.Receipt(messageId);

            return this.Ok(message);
        }

        ///// <summary>
        ///// 将指定的消息标记为已读。
        ///// </summary>
        ///// <param name="messageId">消息编号。</param>
        ///// <param name="requestDto"></param>
        ///// <response code="200"></response>
        ///// <response code="204">指定的消息不存在或已被删除。</response>
        ///// <returns></returns>
        //[HttpPost("{message_id}/[action]")]
        //[ProducesResponseType(typeof(MessageDto), 200)]
        //public IActionResult MarkAsRead([FromRoute(Name = "message_id")] long messageId, [FromBody] MarkAsReadRequestDto requestDto)
        //{
        //    var message = messageService.MarkAsRead(messageId, requestDto);

        //    return this.Ok(message);
        //}

        /// <summary>
        /// 获取用户未读消息数量。
        /// </summary>
        /// <response code="200">当前用户的未读消息数量。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(UnreadCountDto), 200)]
        public IActionResult UnreadCount()
        {
            var unreadCount = messageService.GetUnreadCount();

            return this.Ok(new UnreadCountDto
            {
                UnreadCount = unreadCount
            });
        }
    }
}
